<?php
class Application_Model_Students extends Zend_Db_Table_Abstract
{
    protected $_name = "students";

    /**
     * Creates the user from the data provided
     *
     * @param  $data the data to be inserted
     * @return bool|mixed
     */
    public function createUser($data)
    {
        if (empty($data['id']) || empty($data['pwd']) || $this->studentExists($data['id'])) {
            return FALSE;
        } else {
            $data['pwd'] = md5($data['pwd']);
            return $this->insert($data);
        }
    }

    /**
     * Checks Auth based on the student id and password
     * @param  $id
     * @param  $pwd
     * @return void
     */
    public function checkLogin($id, $pwd)
    {
        $data = $this->fetchRow("id='{$id}'");
        if ($data) {
            if (md5($pwd) == $data['pwd']) {
                return TRUE;
            } else {
                return FALSE;
            }
        } else {
            return FALSE;
        }
    }

    /**
     * Fetches the user data based on the user id
     * @param int  $id the userid
     * @return bool|null|Zend_Db_Table_Row_Abstract
     */

    public function getUserData($id)
    {
        $data = $this->fetchRow("id='{$id}'");
        if ($data) {
            return $data;
        } else {
            return FALSE;
        }
    }


    /**
     * Check if the student exists already
     * @param  $id
     * @return bool
     */

    function studentExists($id) {
        $data = $this->fetchRow("id='{$id}'");
        if(!empty($data['id'])) {
            return TRUE;
        } else {
            return FALSE;
        }
    }


}